package com.siyao.redvip.components.view;

import java.util.ArrayList;
import java.util.List;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.siyao.redvip.R;
import com.siyao.redvip.image.ImageLoaderOptions;
import com.siyao.redvip.utils.AppUtils;

import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.content.Context;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.view.View.OnClickListener;

public class MoreImageView extends LinearLayout implements OnClickListener{
	
	private List<String> paths = new ArrayList<String>();//放图片路径list
	private String path;
	private View imgs1, imgs2,imgs3, imgs4;//图片外面的LinearLayout
	private ImageView iv1, iv2, iv3, iv4, iv5, iv6, iv7, iv8;
	public DisplayImageOptions options;
	private Context context;
	
	public MoreImageView(Context context) {
		super(context);
		init(context);
	}
	
	public MoreImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }
	
	
	private void init(Context context) {
		this.context = context;
		options=ImageLoaderOptions.getDefaultInstance();
		View contentView = LayoutInflater.from(context).inflate(R.layout.layout_moreimages_view, null);
		imgs1=contentView.findViewById(R.id.llImgs1);
		imgs2=contentView.findViewById(R.id.llImgs2);
		imgs3=contentView.findViewById(R.id.llImgs3);
		imgs4=contentView.findViewById(R.id.llImgs4);
		
		iv1=(ImageView) contentView.findViewById(R.id.ivImage1);
		iv2=(ImageView) contentView.findViewById(R.id.ivImage2);
		iv3=(ImageView) contentView.findViewById(R.id.ivImage3);
		iv4=(ImageView) contentView.findViewById(R.id.ivImage4);
		iv5=(ImageView) contentView.findViewById(R.id.ivImage5);
		iv6=(ImageView) contentView.findViewById(R.id.ivImage6);
		iv7=(ImageView) contentView.findViewById(R.id.ivImage7);
		iv8=(ImageView) contentView.findViewById(R.id.ivImage8);
		
	 	iv1.setOnClickListener(this);
        iv2.setOnClickListener(this);
        iv3.setOnClickListener(this);
        iv4.setOnClickListener(this);
        iv5.setOnClickListener(this);
        iv6.setOnClickListener(this);
        iv7.setOnClickListener(this);
        iv8.setOnClickListener(this);
        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        this.addView(contentView,layoutParams);
	}
	
	public void addPaths(List<String> paths) {
        if (this.paths != null && !this.paths.isEmpty()) {
            this.paths.addAll(paths);
        }
        setPaths(this.paths);
    }

    public List<String> getPaths() {
        return paths;
    }

    public void setPaths(List<String> paths) {
        if (paths == null || paths.isEmpty()) {
            return;
        }
        clear();
        this.paths.addAll(paths);
        this.setVisibility(View.VISIBLE);
        if (!TextUtils.isEmpty(paths.get(0))) {
            showImg(paths.get(0), iv1);
            imgs1.setVisibility(View.VISIBLE);
        }
        if (paths.size() >= 2 && !TextUtils.isEmpty(paths.get(1))) {
            showImg(paths.get(1), iv2);
        }
        if (paths.size() >= 3 && !TextUtils.isEmpty(paths.get(2))) {
            showImg(paths.get(2), iv3);
            imgs2.setVisibility(View.VISIBLE);
        }
        if (paths.size() >= 4 && !TextUtils.isEmpty(paths.get(3))) {
            showImg(paths.get(3), iv4);
        }
        if (paths.size() >= 5 && !TextUtils.isEmpty(paths.get(4))) {
            showImg(paths.get(4), iv5);
            imgs3.setVisibility(View.VISIBLE);
        }
        if (paths.size() >= 6 && !TextUtils.isEmpty(paths.get(5))) {
            showImg(paths.get(5), iv6);
        }
        if (paths.size() >= 7 && !TextUtils.isEmpty(paths.get(6))) {
            showImg(paths.get(6), iv7);
            imgs4.setVisibility(View.VISIBLE);
        }
        if (paths.size() >= 8 && !TextUtils.isEmpty(paths.get(7))) {
            showImg(paths.get(7), iv8);
        }
    }
    
    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        clear();
        if (TextUtils.isEmpty(path)) {
            return;
        }
        this.path = path;
        this.paths.set(0, path);
        showSingleImg();
    }
    
    private void showSingleImg() {
        imgs1.setVisibility(View.VISIBLE);
        imgs2.setVisibility(View.GONE);
        imgs3.setVisibility(View.GONE);
        imgs4.setVisibility(View.GONE);
        showImg(path, iv1);
    }
    
    private void toImgDetial(int position) {
        if (editImageCallback != null) {
            editImageCallback.editImage(position);
        } else {
            AppUtils.toImageDetial(context, position, paths);
        }
    }
    

    private void showImg(String path, ImageView iv) {
        iv.setVisibility(View.VISIBLE);
        if (path.startsWith("http") || path.startsWith("file://")) {
            ImageLoader.getInstance().displayImage(path, iv, options);
        } else {
            path = "file://" + path;
            ImageLoader.getInstance().displayImage(path, iv, options);
        }
    }
	
    public void clear() {
        this.path = "";
        this.paths.clear();
        iv1.setImageResource(0);
        iv2.setImageResource(0);
        iv3.setImageResource(0);
        iv4.setImageResource(0);
        iv5.setImageResource(0);
        iv6.setImageResource(0);
        iv7.setImageResource(0);
        iv8.setImageResource(0);
        iv1.setVisibility(View.INVISIBLE);
        iv2.setVisibility(View.INVISIBLE);
        iv3.setVisibility(View.INVISIBLE);
        iv4.setVisibility(View.INVISIBLE);
        iv5.setVisibility(View.INVISIBLE);
        iv6.setVisibility(View.INVISIBLE);
        iv7.setVisibility(View.INVISIBLE);
        iv8.setVisibility(View.INVISIBLE);
        imgs1.setVisibility(View.GONE);
        imgs2.setVisibility(View.GONE);
        imgs3.setVisibility(View.GONE);
        imgs4.setVisibility(View.GONE);
    }

	@Override
	public void onClick(View v) {
        switch (v.getId()) {
        case R.id.ivImage1:
            toImgDetial(0);
            break;
        case R.id.ivImage2:
            toImgDetial(1);
            break;
        case R.id.ivImage3:
            toImgDetial(2);
            break;
        case R.id.ivImage4:
            toImgDetial(3);
            break;
        case R.id.ivImage5:
            toImgDetial(4);
            break;
        case R.id.ivImage6:
            toImgDetial(5);
            break;
        case R.id.ivImage7:
            toImgDetial(6);
            break;
        case R.id.ivImage8:
            toImgDetial(7);
            break;

        default:
            break;
    }
}
	
	public void shouldVisibale() {
        if (this.paths != null && this.paths.size() > 0) {
            this.setVisibility(View.VISIBLE);
        } else {
            this.setVisibility(View.GONE);
        }
    }
	
    private EditImageCallback editImageCallback;

    public interface EditImageCallback {
        void editImage(int position);
    }

    public void setEditImageCallback(EditImageCallback editImageCallback) {
        this.editImageCallback = editImageCallback;
    }
}
